package com.kotlin.flong.reflection

import kotlin.reflect.KMutableProperty
import kotlin.reflect.full.memberProperties

/**
 * @Author:liangjl
 * @Date: 2020/7/12-8:24
 * @Eamil:jilongliang@sina.com
 * @Description: oop_fp里面用到反射
 * 通过反射去修改对象属性的值
 *
 */
class MyTest9 {
    var name: String = "周伯通"
    var authorName: String = "一灯大师"
}

fun main() {
    var myTestClass = MyTest9::class
    var myTestObj = MyTest9()

    var variableInvoke = myTestClass.memberProperties.find { it.name == "name" }
    println(variableInvoke?.get(myTestObj))

    if (variableInvoke is KMutableProperty<*>) {
        //通过反射去修改对象属性值
        variableInvoke.setter.call(myTestObj,"杨过")
    }
    println(variableInvoke?.get(myTestObj))
}